package com.example.sellqq.service.impl;

import com.example.sellqq.domain.AddressBook;
import com.example.sellqq.repository.AddressBookRepository;
import com.example.sellqq.service.AddressBookService;
import com.example.sellqq.vo.ResponseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: thekernel
 * @Date: 2020/4/24 22:39
 * @Version: 1.0
 * @Description:
 */
@Service(value = "AddressBookService")
public class AddressBookServiceImpl implements AddressBookService {

    @Autowired
    private AddressBookRepository addressBookRepository;

    /**
     * 保存多条通讯录
     *
     * @param addressBooks 通讯录
     * @return 保存是否成功
     */
    @Override
    public ResponseVO saveAddressBook(List<AddressBook> addressBooks) {
        this.addressBookRepository.saveAll(addressBooks);
        return ResponseVO.success("添加成功");
    }

    /**
     * 获取所有通讯录
     *
     * @return 通讯录内容
     */
    @Override
    public ResponseVO getAllAddressBook() {
        List<AddressBook> addressBooks = this.addressBookRepository.findAll(Sort.by(Sort.Direction.DESC, "createTime"));
        return ResponseVO.success("获取通讯录成功", addressBooks);
    }
}
